#!/usr/bin/bash

# 安装k8s
initK8s() {
    psh hd001-hd100 "/opt/daq/k8s/init-ks8/install.sh"
}

# 拷贝kubeadm(修改过源码)、hosts文件、containerd配置
copyK8s() {
    # psh hd001-hd100 "mkdir -p /usr/lib/systemd/system/kubelet.service.d/;mkdir -p /etc/containerd/;mkdir -p /var/lib/kubelet/"
    cat /etc/hosts | while read line
    do
        array=(${line//,/ })
        host=${array[1]}
        if [[ "$host" =~ ^hd.* ]]
        then
            # echo $host
            # scp -qpr /etc/hosts $host:/etc/hosts
            # scp -qpr /usr/bin/kubeadm $host:/usr/bin/kubeadm
            # scp -qpr /etc/containerd/config.toml $host:/etc/containerd/config.toml
            # scp -qpr /usr/bin/crictl $host:/usr/bin/crictl
            # scp -qpr /var/lib/kubelet/kubeadm-flags.env $host:/var/lib/kubelet/kubeadm-flags.env
            # scp -qpr /usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf $host:/usr/lib/systemd/system/kubelet.service.d/10-kubeadm.conf
            scp cni.tar $host:/root/
        fi
    done
}

# 加入k8s节点
joinK8s() {
    # psh hd001-hd100 "kubeadm reset -f;systemctl daemon-reload;systemctl enable kubelet && systemctl start kubelet"
    psh hd001-hd100 "kubeadm join 10.3.192.10:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:4373a12bb8c746f7027978c6534403146e76dc2894b7c6eb6e5ae30b0a9b9c41 \
    --cri-socket /run/containerd/containerd.sock"
}

labelK8s() {
    i=1
    cat /etc/hosts | while read line
    do
        array=(${line//,/ })
        host=${array[1]}
        if [[ "$host" =~ ^hd.* ]];then
            if ((i <= 33));then
                kubectl label nodes $host dataflow.daq=ros
            elif ((33 < i)) && ((i <= 66));then
                kubectl label nodes $host dataflow.daq=eb
            elif ((66 < i)) && ((i <= 99));then
                kubectl label nodes $host dataflow.daq=ps
            else
                kubectl label nodes $host dataflow.daq=es
            fi
            ((i=i+1))
        fi
    done
}

# copyK8s
# initK8s
# joinK8s
# labelK8s